/**
 * error message
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2008. 08. 24
*/
package com.cmiscm.utils.display
{
	import com.cmiscm.utils.common.DisplayObjectUtilities;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	/**
	 * Error Message.
	 * 
	 * @example
 	 * <listing> 
 	 * package {
     *    import flash.display.Sprite;
     *    import com.cmiscm.utils.display.ErrorMsg;
     *    import flash.events.Event;
     *    
     *    public class TestError extends Sprite
     *    {
     *        public function TestError()
     *        {
     *             super();
     *             this.addEventListener(Event.ADDED_TO_STAGE, onAdd);
     *         }
     * 
     *         private function onAdd(evt:Event):void
     *         {
     *             evt.currentTarget.removeEventListener(evt.type, arguments.callee);
     * 
     *             var error:ErrorMsg = new ErrorMsg(ErrorMsg.ERROR_CASE_CONFIG);
     *             this.addChild(error);
     *        }
     *    }
     * }
     * </listing> 
     * <p>
 	 * Result:</p>
 	 * <img src = "ErrorMsg0.gif" />
     */	
	public class ErrorMsg extends Sprite
	{
		/**
		 * failed to load config xml
		 */		
		public static const ERROR_CASE_CONFIG:String = "failed to load config xml<br><font color='#999999'>please refresh the page</font>";
		
		/**
		 * failed to load swf data file
		 */		
		public static const ERROR_CASE_SWF:String = "failed to load swf data file<br><font color='#999999'>please refresh the page</font>";
		
		/**
		 * failed to load font data file
		 */		
		public static const ERROR_CASE_FONT:String = "failed to load font data file<br><font color='#999999'>please refresh the page</font>";
		
		private static var _bgSprite:Sprite;
		private static var _mc:Sprite;
		
		/**
		 * Show error message 
		 * @param $str - Message
		 * @param $color - Font color
		 * @param $bgColor - Bg color
		 * @param $mc - Moviclip
		 * @param $multi - Textfield multiline
		 * 
		 */		
		public function ErrorMsg($str:String, $color:uint=0x000000, $bgColor:uint=0xFFFFFF, $mc:Sprite=null, $multi:Boolean=true)
		{
			super();
			setting($str, $color, $bgColor, $mc, $multi);
		}
		
		private function setting($str:String, $color:uint, $bgColor:uint, $mc:Sprite, $multi:Boolean):void
		{
			_bgSprite = new Sprite();
			_bgSprite.graphics.clear();
			_bgSprite.graphics.beginFill($bgColor, 1);
			_bgSprite.graphics.drawRect(0, 0, 100, 100);
			_bgSprite.graphics.endFill();
			
			if ($mc != null) {
				_mc = $mc;				
				_mc["txt"].defaultTextFormat = _mc["txt"].getTextFormat();
				_mc["txt"].autoSize = TextFieldAutoSize.LEFT;
				_mc["txt"].multiline = $multi;
				_mc["txt"].htmlText = $str;
				
				if(_mc["logo"] != undefined) {
					_mc["logo"].y = int(_mc["txt"].height + 15);
					_mc["logo"].x = int((_mc["txt"].width - _mc["logo"].width) * .5);
				}				
			} else {
				_mc = new Sprite();
				
				var formatter:TextFormat = new TextFormat();
				formatter.font = "_sans";
				formatter.size = 11;
				formatter.align = TextFormatAlign.CENTER;
				formatter.color = $color;
				
				var msgTxt:TextField = new TextField();
				msgTxt.defaultTextFormat = formatter;
				msgTxt.autoSize = TextFieldAutoSize.LEFT;
				msgTxt.multiline = $multi;
				msgTxt.htmlText = $str;
				
				_mc.addChild(msgTxt);
			}
			this.addEventListener(Event.ADDED_TO_STAGE, onAdd);					
		}
		
		private function onAdd(evt:Event):void
		{
			evt.currentTarget.removeEventListener(evt.type, arguments.callee);
			
			this.stage.align = StageAlign.TOP_LEFT;
			
			this.stage.addChild(_bgSprite);
			this.stage.addChild(_mc);
			
			this.stage.addEventListener(Event.RESIZE, onResize);
			onResize(null);
		}
		
		private function onResize(event:Event):void
		{
			_bgSprite.width = this.stage.stageWidth;
			_bgSprite.height = this.stage.stageHeight;		
			_mc.x = int((this.stage.stageWidth - _mc.width) >> 1);
			_mc.y = int((this.stage.stageHeight - _mc.height) >> 1);
		}
		
	}
}